// devops/pages/power/collect.js
import service from '../../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeList: [{
      id: 1, name: '箱变采集器'
    },{
      id: 2, name: '配电箱采集器'
    }],
    deviceList: [{id: 1, name: '设备1'}],
    multiData: [],
    acurrent: '',
    amaxCurrent: '',
    aretainCurrent: '',
    bcurrent: '',
    bmaxCurrent: '',
    bretainCurrent: '',
    capacity: '',
    ccurrent: '',
    cmaxCurrent: '',
    cretainCurrent: '',
    type: '',
    retainCapacity: '',
    deviceId: '',
    id: '',
    showMul: false,
    deviceValue: [],
    pidValue: [-99, -99,-99]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.id){
      this.setData({
        id: options.id
      })
      this.getDetail()
    }else{
      this.setData({
        showMul: true
      })
    }
   
  },
  getDetail(){
    let that = this
    service("GET",`/devops/power/box/collector/info/${this.data.id}`).then(res => {
      console.log('info', res)
      if(res.data.code == 0){
        let info = res.data.info
        that.getDevice(info.deviceId)
        that.setData({
          acurrent: info.aCurrent,
          amaxCurrent: info.aMaxCurrent,
          aretainCurrent: info.aRetainCurrent,
          bcurrent: info.bCurrent,
          bmaxCurrent: info.bMaxCurrent,
          bretainCurrent: info.bRetainCurrent,
          capacity: info.capacity,
          ccurrent: info.cCurrent,
          cmaxCurrent: info.cMaxCurrent,
          cretainCurrent: info.cRetainCurrent,
          type: info.type,
          retainCapacity: info.retainCapacity,
          deviceId: info.deviceId,
          showMul: true,
          // pidArray: [1,2]

        })
      }
    })
  },

  
  getmultiData(){
    let that = this
    service('POST', '/devops/power/box/device/list/all',{pid: 0}).then((res) => {
      console.log('第一级设备', res)
      that.setData({
        firstMultiData: res.data.list
      })
      let provinceList = [...res.data.list]  //放到一个数组里面
      let provinceArr = res.data.list.map((item)=>{return item.name})  //获取名称
      console.log('provinceArr', provinceArr)
      that.setData({
        multiArray:[provinceArr,[],[]], //更新三维数组  更新完为[['广东','北京'],[],[]]
        province_list:provinceList,  //省级原始数据
        province_name:provinceArr,  //省级全部名称
      })
      let defaultCode = that.data.province_list[0].id //使用第一项当作参数获取市级数据
      if(defaultCode){
        that.setData({
          currnetProvinceId:defaultCode  //保存当前省份id
        })
        that.getCity(defaultCode) //获取市区数据
      }
    })
    service('POST', `/devops/power/box/device/child/1`,{
    }).then((res) => {
      console.log('省', res)
      let cityArr = res.data.list.map((item)=>{return item.name}) //返回城市名称
      let cityList = [...res.data.list]
      that.setData({
        multiArray:[that.data.province_name,cityArr,[]],  //更新后[['广东','北京'],['潮州'，'汕头','揭阳'],[]]
        city_list:cityList, //保持市级数据
        city_name:cityArr   //市级名称
      })
      let defaultCode = that.data.city_list[0].id //获取第一个市的区级数据
      if(defaultCode){
        that.setData({
          currentCityId:defaultCode //保存当下市id
        })
        that.getArea(defaultCode) //获取区域数据
      }
    })
  },
  getDevice(id){
    let that = this
    service("POST",`/devops/power/box/device/parent/${id}`).then(res => {
      console.log('info', res)
      if(res.data.code == 0){
        let data = res.data.info
        let arr = [id, -99, -99]
        // if(data.id){
        //   arr = [data.id, ...arr]
        // }
        if(data.parent){
          arr = [data.parent.id, id, -99]
          if(data.parent.parent){
            arr = [data.parent.parent.id, data.parent.id, id]
          }
        }
        that.setData({
          pidValue: arr
        })
      }
    })
  },
  confirmType(option){
    console.log('option.detail',option.detail)
    this.setData({
      type: this.data.typeList[option.detail].id,
    })

  },
  confirmDevice(option){
   
    let arr = option.detail
    let pid = 0
    if(arr.one !=-99){
      pid = arr.one
    }
    if(arr.two !=-99){
      pid = arr.two
    }
    if(arr.three !=-99){
      pid = arr.three
    }
    console.log('confirmDevice', pid)
    this.setData({
      deviceId: pid,
    })
  },
  confirmPost(){
    let retainCapacity = this.data.retainCapacity,  acurrent = this.data.acurrent, amaxCurrent= this.data.amaxCurrent, aretainCurrent= this.data.aretainCurrent, bcurrent =this.data.bcurrent, bmaxCurrent = this.data.bmaxCurrent, bretainCurrent = this.data.bretainCurrent, capacity = this.data.capacity, ccurrent = this.data.ccurrent, cmaxCurrent = this.data.cmaxCurrent, cretainCurrent = this.data.cretainCurrent, type = this.data.type, deviceId = this.data.deviceId
    var id = this.data.id
    console.log('cc', { type, deviceId, retainCapacity ,acurrent,amaxCurrent,aretainCurrent, bcurrent, bmaxCurrent, bretainCurrent, capacity, ccurrent, cmaxCurrent, cretainCurrent, id })
    // return
    
    let url = id ? "/devops/power/box/collector/update" : "/devops/power/box/collector/add"
    let postData = id ? { type, deviceId, retainCapacity ,acurrent,amaxCurrent,aretainCurrent, bcurrent, bmaxCurrent, bretainCurrent, capacity, ccurrent, cmaxCurrent, cretainCurrent, id } : { type, deviceId, retainCapacity ,acurrent,amaxCurrent,aretainCurrent, bcurrent, bmaxCurrent, bretainCurrent, capacity, ccurrent, cmaxCurrent, cretainCurrent}
    service("POST",url,postData).then(res => {
      console.log('info', res)
      if(res.data.code == 0){
        wx.showToast({
          title: '操作成功',
          icon: 'none'
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 1000);
      }else{
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})